package io.renren.modules.zhaopin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.renren.common.utils.R;
import io.renren.modules.zhaopin.dao.BackgroundCheckOrdersDao;
import io.renren.modules.zhaopin.entity.BackgroundCheckOrdersEntity;
import io.renren.modules.zhaopin.entity.BackgroundCustomInfoEntity;
import io.renren.modules.zhaopin.entity.BackgroundItemEntity;
import io.renren.modules.zhaopin.service.BackgroundCheckOrdersService;
import io.renren.modules.zhaopin.service.BackgroundCustomInfoService;
import io.renren.modules.zhaopin.service.BackgroundItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

@Service("backgroundCheckOrdersService")
public class BackgroundCheckOrdersServiceImpl extends ServiceImpl<BackgroundCheckOrdersDao, BackgroundCheckOrdersEntity> implements BackgroundCheckOrdersService {

    @Autowired
    private BackgroundCustomInfoService backgroundCustomInfoService;
    @Autowired
    private BackgroundItemService backgroundItemService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BackgroundCheckOrdersEntity> page = this.page(
                new Query<BackgroundCheckOrdersEntity>().getPage(params),
                new QueryWrapper<BackgroundCheckOrdersEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R getOrderItem(String reportId) {

        LambdaQueryWrapper<BackgroundCheckOrdersEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BackgroundCheckOrdersEntity::getReportId, reportId);
        BackgroundCheckOrdersEntity checkOrders = this.getOne(queryWrapper);
        if (checkOrders == null) {
            return R.ok();
        }
        String customItemId = checkOrders.getCustomItemId();
        LambdaQueryWrapper<BackgroundCustomInfoEntity> bcInfoQueryWrapper = new LambdaQueryWrapper<>();
        bcInfoQueryWrapper.eq(BackgroundCustomInfoEntity::getCustomItemId, customItemId);
        BackgroundCustomInfoEntity customInfoEntities = backgroundCustomInfoService.getOne(bcInfoQueryWrapper);
        if (customInfoEntities == null) {
            return R.ok().put("data", checkOrders);
        }
        // 背调项id
        String chooseList = customInfoEntities.getChooseList();
        String[] chooseId = chooseList.split(",");

        // LambdaQueryWrapper<BackgroundItemEntity> biQw = new LambdaQueryWrapper<>();
        // biQw.in(BackgroundItemEntity::getItemId, Arrays.asList(chooseId));
        // List<BackgroundItemEntity> list = backgroundItemService.list(biQw);
        return R.ok().put("data", chooseId).put("info", checkOrders);
    }

}